//
//  GXWTabViewController.swift
//  swift3.0项目开始
//
//  Created by 龚昕炜 on 16/7/27.
//  Copyright © 2016年 龚昕炜. All rights reserved.
//

import UIKit

class GXWTabViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpChildViewCOntrollers()
        
        setUpComposeButton()
    }
    
    /** 支持的设备方向，好处，可以在需要横屏的时候单独处理。 */
    /** 当前控制器和子控制器都会遵守， 因为视频播放是modal展示的 所以只遵守项目中设置的三个方向， 而不会继承这个只支持一个方向的 */
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .portrait
    }
    
    /** 自定义button */
    private lazy var composeButton: UIButton = { ()->UIButton in

        let button = UIButton()
        
        button.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
        
        button.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        button.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
        
        return button
    }()
    
    /** 撰写微博按钮；objc是保证它能用oc的消息机制调用；pricate是保证它是私有的，只有当前文件可以访问到 */
    @objc private func composeStatus() {
        
        let vc = UIViewController()
        
        let nav = UINavigationController(rootViewController:vc)
        
        present(nav, animated: true, completion: nil)
    }

}

/** 便于维护， 将功能相近函数放在里面，分隔代码(不能定义属性) */
extension GXWTabViewController {

    /** 添加撰写按钮 */
    private func setUpComposeButton() {
        
        tabBar.addSubview(composeButton)
        
        let count = CGFloat(childViewControllers.count)
        
        /** －1是为了把容错点挡住，-1让他的宽度增大，遮住容错点 */
        let w = tabBar.bounds.width / count - 1
        /** insetBy是向内缩进，从两边向内缩进2个button距离，那他的宽度就是一个button的宽度 */
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
    }
    
    /** 添加子控制器 */
    private func setUpChildViewCOntrollers() {
    
        let array = [
            ["clsName": "GXWHomeViewController", "title": "首页", "imageName": "home"],
            ["clsName": "GXWMessageViewController", "title": "消息", "imageName": "message_center"],
            ["clsName": "UIViewController"],
            ["clsName": "GXWDiscoverViewController", "title": "发现", "imageName": "discover"],
            ["clsName": "GXWProfileViewController", "title": "我", "imageName": "profile"],
        ]
        
        var arrayM = [UIViewController]()
        
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    
    /** 使用字典创建控制器 */
    private func controller(dict: [String: String]) -> UIViewController {
        
        guard let clsName = dict["clsName"],
            title = dict["title"],
            imageName = dict["imageName"],
            cls = NSClassFromString("swift3_0项目开始" + "." + clsName) as? UIViewController.Type

            else {
                return UIViewController()
        }
        print(cls)
        let vc = cls.init()
        
        vc.title = title
        
        /** 设置图片 */
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_highlighted")?.withRenderingMode(.alwaysOriginal)
        
        /** 设置字体属性，(颜色和字体) --> (tabbar系统默认是12号字体, 默认渲染蓝色) */
        /** 注意不能设置高亮下(选中)下字体的大小，设置是没有效果的 */
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange(), NSFontAttributeName: UIFont.systemFont(ofSize: 15)], for: UIControlState(rawValue:0))
        
        let nav = GXWNavgationController(rootViewController: vc)
        
        return nav
    }

}
